package com.geez.bbl.projector;

import com.geez.bbl.db.type.BblBook;
import com.geez.bbl.db.type.BblChapter;
import com.geez.bbl.db.type.Builder;
import com.geez.bbl.db.type.Verse;
import com.geez.bbl.util.FileUtils;
import com.geez.bbl.util.ProjectResources;

import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * @author biniam.gebremichael
 * @since 6/7/12
 */
public class GenerateIndesignXml {

    static String prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><bible>\n";
    static String postfix = "</bible>\n";
    private static File resourceFile = ProjectResources.getResourceFile("Indesign.xml");
    private static File resourceDir = ProjectResources.getResourceFile("xmlBooks");
    //     "<BIBLEBOOK bnumber=\"1\" bname=\"Genesis\">"


    public static void main(String[] args) throws Exception {
        List<BblBook> books = new Builder("ti").build();
        if (args.length > 0) {
            resourceFile = new File(args[0]);
        }
        new GenerateIndesignXml().createXml(books, resourceFile);
        separteBooks(books);
        System.out.println("indesign xml written to " + resourceFile.getAbsolutePath());
    }

    private static void separteBooks(List<BblBook> books) {
        resourceDir.mkdir();
        for (BblBook book : books) {
            new GenerateIndesignXml().createXml(Arrays.asList(book), new File(resourceDir,"book"+book.getBook_no()+".xml"));
        }
    }


    public void createXml(List<BblBook> books, File resourceFile) {
        StringBuilder builder = new StringBuilder();
        builder.append(prefix);
        for (BblBook book : books) {
            builder.append(getBook(book));
        }
        builder.append(postfix);
        FileUtils.save(resourceFile, builder.toString());

    }

    private String getBook(BblBook book) {
        StringBuilder builder = new StringBuilder();
        builder.append("<book>\n").
                append("<bookHeading aid:pstyle=\"bookHeading\">").
                append( book.getLong_name()).
                append("</bookHeading>\n").
                append("<bookContent>");
        for (BblChapter chapter : book.getChapters().values()) {
            builder.append(getChapter(chapter));
        }
        builder.append("</bookContent>");

        builder.append("\t</book>\n");
        return builder.toString();
    }

    private String getChapter(BblChapter chapter) {
        StringBuilder builder = new StringBuilder();
        builder.append("\n<chapter><chapterHeading aid:pstyle=\"chapterHeading\"><chapterName aid:cstyle=\"chapterName\">ምዕራፍ </chapterName><chapterNumber aid:cstyle=\"chapterNumber\">").
                append(chapter.getId()).
                append("</chapterNumber></chapterHeading>\n").
                append("<chapterContent>\n<paragraph aid:pstyle=\"paragraph\">");
        for (Verse verse : chapter.getVerses()) {
               builder.append(getVerses(verse));
        }
        builder.append("</paragraph>\n</chapterContent>\n</chapter>");
        return builder.toString();
    }

    private static String getVerses(Verse verse) {
        StringBuilder builder = new StringBuilder();
        String num = (verse.getStartId()==verse.getEndId())?String.valueOf(verse.getStartId()):""+verse.getStartId()+"-"+verse.getEndId();
        builder.append("<verse><verseNumber aid:cstyle=\"verseNumber\">").
                append(num).
                append("</verseNumber><verseValue>")
                .append(verse.getValue()).append("</verseValue></verse>");
        return builder.toString();
    }


}
